<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/8/15
 * Time: 16:38
 */

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use yii\db\query;
use yii\filters\Cors;
use yii\helpers\ArrayHelper;
use yii\web\models;
use \common\helps\tools;//使用工具类
use yii\db\ActiveRecord;
use yii\db\Connection;
use app\models\Upload;
use yii\web\UploadedFile;

/**
 * ResearchOther controller
 */

class ResearchOtherController extends Controller
{
    public function init()
    {
        $this->enableCsrfValidation = false;
    }




    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * title 查询所有进度
     * @return string
     */
    public function actionSelectPortPlan()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_'.$uid,$token);
        if($checkToken != 4){
            return tools::ajax_return('9','token过期,请重新登录',[$checkToken]);
        }

        $plan = new \app\models\ProjectPlan;
        $result = $plan->selectPlan();
        if(!$result){
            return tools::ajax_return('0','成功',[]);
        }

        return tools::ajax_return('0','成功',$result);


    }

    /**
     * title 上传文件
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionUpFile()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $uid = $post['uid'];
        $token = $post['token'];
        //token验证
        $checkToken = tools::checkToken('admin_' . $uid, $token);
        if ($checkToken != 4) {
            return tools::ajax_return('2', 'token过期,请重新登录', [$checkToken]);
        }

        $file = $_FILES['file'];//文件名称
        $id = $_FILES['id'];//列表id
        $type = $_FILES['type'];//类型 哪种pdf

        $imgname = $file["name"]; //获取上传的文件名称
        $filetype = pathinfo($imgname, PATHINFO_EXTENSION);//获取后缀

        //条件判断
        if(empty($id)){
            return tools::ajax_return_error('2','id不能为空',[]);
        }
        if(empty($type)){
            return tools::ajax_return_error('2','请选择类型',[]);
        }
        if(empty($filetype)){
            return tools::ajax_return_error('2','请上传文件',[]);
        }


        $table = 'd_'.$type;
        /**
         * 文件上传
         * 我们这里上传成功后把图片的地址进行返回
         */
        $redis = Yii::$app->cache;
        $accessToken = $redis->get('admin_' . $uid);
        //用户名
        $fnum = $accessToken['phone'];
        //当日日期
        $date = date('Y-m-d', time());
        //文件存储路径
        $file_dir = "uploads/img/" . $fnum . "/" .$type. "/" . $date;
        if (!is_dir($file_dir)) {
            $res = mkdir(iconv("UTF-8", "GBK", $file_dir), 0755, true);
        }



        $newname = md5(time() . $imgname) . "." . $filetype; //构建新名称

        if (empty($filetype)) {
            return tools::ajax_return_error('2', '请传文件', []);
        }
        //文件不为空
        if (!empty($filetype)) {
            $file_dir = $file_dir . "/" . $newname;
            //数据库存储路径
            $realpath = "admin/uploads/img/" . $fnum . "/" . $type . "/" . $date . "/" . $newname;
            //保存图片
            move_uploaded_file($file['tmp_name'], $file_dir);

            $other = new \app\models\Research;
            $create_time = date('Y-m-d H:i:s', time());
            //保存示范应用表
            $addThesis = $other->saveFile($id, $create_time, $realpath, $uid);
            if (!$addThesis) {
                return tools::ajax_return_error('2', '保存附件失败', [1]);
            }

            //存附件详情表
            $thesis = new \app\models\Thesis;
            $addFileDetail = $thesis->addFileDetail($imgname, $table, $id, $filetype, $create_time, $uid);
            if (!$addFileDetail) {
                return tools::ajax_return_error('2', '保存附件失败', [2]);
            }
        }



    }






}